import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/personnel_department_tree/personnel_department_tree_controller.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/common/department_tree_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/common/department_user_entity.dart';

/// 人员部门树状选择
class PersonnelDepartmentTreePage extends StatelessWidget {
  PersonnelDepartmentTreePage({super.key});

  final controller = Get.put(PersonnelDepartmentTreeController());

  /// 部门下人员信息
  /// [userEntity] 人员数据
  /// [level] 所在层级
  Widget _getUserTreeView(DepartmentUserEntity userEntity, int level) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          constraints: BoxConstraints(
            minHeight: 55.w,
          ),
          padding: EdgeInsets.symmetric(vertical: 10.w),
          child: Row(
            children: [
              // 间距
              SizedBox(width: 20.w * level),
              // 占位
              SizedBox(width: 40.w),
              Expanded(
                child: Text(
                  userEntity.getShowName(),
                  style: TextStyle(
                    fontSize: 14.sp,
                    fontWeight: FontWeight.w400,
                    color: const Color(0xff333333).withOpacity(0.5),
                  ),
                ),
              ),
              // 分割线
              Container(
                margin: EdgeInsets.symmetric(horizontal: 10.w),
                height: 20.w,
                width: 1.w,
                color: const Color(0xffDDE6F0),
              ),
              SizedBox(
                width: 80.w,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    SvgPicture.asset(
                      controller.userIsSelected(userEntity) ? AppImages.icon_selection_box : AppImages.icon_un_selection_box,
                      width: 16.w,
                      height: 16.w,
                    ),
                    AppGaps.hGap8,
                    Text(
                      '选择',
                      style: TextStyle(
                        fontSize: 14.sp,
                        fontWeight: FontWeight.w400,
                        color: const Color(0xff5C6DFF),
                      ),
                    )
                  ],
                ),
              ),
            ],
          ),
        ).clickOnce(() {
          controller.selectPersonnel(userEntity);
        }),
        Divider(
          height: 0.1.w,
          color: const Color(0xffECECEC),
        ),
      ],
    );
  }

  /// 部门数据每一项的显示
  /// [treeEntity] 当前节点数据
  /// [level] 所在层级
  Widget _getTreeView(DepartmentTreeEntity treeEntity, int level) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          constraints: BoxConstraints(
            minHeight: 55.w,
          ),
          padding: EdgeInsets.symmetric(vertical: 10.w),
          child: Row(
            children: [
              // 间距
              SizedBox(width: 20.w * level),
              SizedBox(
                width: 40.w,
                child: SvgPicture.asset(
                  treeEntity.isExpand! ? AppImages.icon_arrow_drop : AppImages.icon_arrow_right,
                  width: 12.w,
                  height: 12.w,
                ),
              ),
              Expanded(
                child: Text(
                  treeEntity.departmentName!,
                  style: TextStyle(
                    fontSize: 15.sp,
                    fontWeight: FontWeight.w500,
                    color: const Color(0xff333333),
                  ),
                ),
              ),
            ],
          ),
        ).clickOnce(() {
          controller.clickOnNode(treeEntity);
        }),
        Divider(
          height: 0.1.w,
          color: const Color(0xffDDE6F0),
        ),
        AnimatedSize(
          duration: const Duration(milliseconds: 500),
          // curve: Curves.easeInExpo,
          curve: Curves.easeInOutExpo,
          child: SizedBox(
            height: treeEntity.isExpand! ? null : 0,
            child: Column(
              children: [
                // 组织部门
                ...treeEntity.children!.map((item) {
                  return _getTreeView(item, level + 1);
                }),
                // 人员
                ...treeEntity.userList!.map((item) {
                  return _getUserTreeView(item, level + 1);
                })
              ],
            ),
          ),
        ),
        // Visibility(
        //   visible: treeEntity.isExpand!,
        //   child: Column(
        //     children: [
        //       // 组织部门
        //       ...treeEntity.children!.map((item) {
        //         return _getTreeView(item, level + 1);
        //       }).toList(),
        //       // 人员
        //       ...treeEntity.userList!.map((item) {
        //         return _getUserTreeView(item, level + 1);
        //       }).toList()
        //     ],
        //   ),
        // ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar(
        titleText: controller.title,
      ),
      body: GetBuilder(
        init: controller,
        builder: (_) {
          return Column(
            children: [
              Container(
                color: const Color(0xffF5F5F6),
                height: 10.w,
              ),
              Expanded(
                child: SingleChildScrollView(
                    child: Column(
                  children: controller.departmentTreeList.map((item) {
                    return _getTreeView(item, 0);
                  }).toList(),
                )),
              ),
              SizedBox(height: 70.w)
            ],
          );
        },
      ),
      bottomSheet: Container(
        padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 5.w),
        decoration: BoxDecoration(
          color: Colors.white,
          boxShadow: [
            BoxShadow(
              color: const Color(0xffBCBCC5).withOpacity(0.3), // 阴影颜色
              offset: const Offset(0, -5), // 阴影的偏移量，向上偏移，即上方阴影
              blurRadius: 10.0, // 阴影的模糊半径
              spreadRadius: 2.0, // 阴影的扩散半径
            ),
          ],
        ),
        child: Row(
          children: [
            Expanded(
              child: ElevatedButton(
                style: ElevatedButton.styleFrom(
                  backgroundColor: AppColors.blue_button,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(22.r),
                  ),
                ),
                onPressed: controller.complete,
                child: Text(
                  '确定',
                  style: TextStyle(
                    fontSize: 16.sp,
                    color: Colors.white,
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
